/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package mapping;
import java.sql.ResultSet;
import mapping.SqlLib;
import entreprise.Drug;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
 *
 * @author duonghau
 */
public class DBSessionDrug {
    public boolean createSessionDrug(int sessionID, String[] drugIDs){
        String insertQuery;
        SqlLib connec = new SqlLib();
        if(connec.initConnection()){
            for(String drugID:drugIDs){
                insertQuery = "INSERT INTO `session-drug` (sessionID, drugID,quantity) VALUES (" + sessionID + "," + drugID+ ",0)";
                connec.execQuery(insertQuery);
            }
           connec.closeConnection();
        }else{
            return false;
        }
        return true;
    }
    public ArrayList<Drug> getDrugs(int sessionID){
        ArrayList<Drug> drugs=new ArrayList<>();
        String selectQuery="select drug.drugID,drug.drugName,drug.drugPrice,drug.drugDescription from drug,`session-drug` where `session-drug`.drugID=drug.drugID and `session-drug`.sessionID="+sessionID;
        SqlLib connec = new SqlLib();
        if (connec.initConnection()) {
            ResultSet sqlResultSet = connec.getDataFromQuery(selectQuery);            
            try {
                while(sqlResultSet.next()) {
                    Drug drug=new Drug();
                    drug.setDrugID(sqlResultSet.getString("drugID"));
                    drug.setDrugName(sqlResultSet.getString("drugName"));
                    drug.setDrugPrice(sqlResultSet.getFloat("drugPrice"));
                    drug.setDrugDescription(sqlResultSet.getString("drugDescription"));
                    drugs.add(drug);
                }
            } catch (SQLException ex) {
                Logger.getLogger(DBDrug.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
//            System.out.println("Drug not found!");
        }           
        return drugs;
    }
}
